<?php

/**
 * @copyright       : FOSP
 * @version         : 20120323
 * @todo            : Model Player Champion 
 * @name            : PlayerChampion 
 * @author          : QuangTM
 */
class PlayerChampion
{

    const PLAYER_CHAMPION_BY_SEASON = 'player_champion_by_season_list';

    protected static $instance;

    protected function __construct ()
    {
        
    }

    /**
     * Get PlayerChampion's instance
     * @return PlayerChampion
     * @author QuangTM
     */
    public static function getInstance ()
    {
        if (!isset(self::$instance) || !self::$instance instanceof self)
        {
            self::$instance = new self;
        }
        return self::$instance;
    }

    /**
     * Get list top players by season
     * @param type $seasonId
     * @return array|boolean 
     * @author QuangTM
     */
    public function getListTopPlayers ($seasonId)
    {
        $memcacheInstance = Fpt_Global::getMemcacheInstance($seasonId);
        $keyCache         = Fpt_Global::makeCacheKey(self::PLAYER_CHAMPION_BY_SEASON, $seasonId);

        // Read from cache
        $arrTopPlayers = $memcacheInstance->read($keyCache);
        // Miss cache
        if ($arrTopPlayers === FALSE)
        {
            try
            {
                $config = Fpt_Global::getApplicationIni();

                // Get db teamseason instance
                $playerChampionObj = Fpt_Business_PlayerChampion::factory($config['database']['default']);

                // Get data from db
                $arrTopPlayers = $playerChampionObj->getListTopPlayers($seasonId);

                // Write to cache
                $memcacheInstance->write($keyCache, $arrTopPlayers);
                Fpt_Global::writeMemcache($seasonId, $keyCache, $arrTopPlayers);
            }
            catch (Exception $ex)
            {
                Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
                return FALSE;
            }
        }
        return $arrTopPlayers;
    }

}